package com.tjetc;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.dom4j.DocumentException;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/8/20
 */
public class TestCustomerMybatis {
    // 测试获取链接
    @Test
    public void testConnection() throws DocumentException, SQLException, ClassNotFoundException {
        MyConfiguration configuration = new MyConfiguration();
        configuration.build("my-config.xml");
    }

    // 测试第一种方法的执行
    @Test
    public void testQuery() throws SQLException, DocumentException, ClassNotFoundException {
        Executor executor = new MyExecutor();
        Bank bank = executor.query("select * from bank where id = ?", 1);
        System.out.println(bank);
    }

    // 测试sqlSession建立连接
    @Test
    public void testSqlSession() throws SQLException, DocumentException, ClassNotFoundException {
        MySqlSession sqlSession = new MySqlSession();
        Bank bank = sqlSession.selectOne("select * from bank where id = ?", 1);
        System.out.println(bank);
    }

    // 测试mapper映射文件
    @Test
    public void testReadMapper() throws DocumentException {
       MyConfiguration configuration = new MyConfiguration();
        MapperBean mapperBean = configuration.readMapper("BankMapper.xml");
        System.out.println(mapperBean);
    }


    // 测试通过sqlSession执行对应sql
    @Test
    public void testSelect(){
        MySqlSession mySqlSession = new MySqlSession();
        BankMapper bankMapper = mySqlSession.getMapper(BankMapper.class);
        Bank bank = bankMapper.selectBankById(1);
        System.out.println("bank = " + bank);
    }


    // 测试会话工厂
    @Test
    public void testSqlSessionFactory(){
        MySqlSession mySqlSession = MySqlSessionFactory.openSqlSession();
        BankMapper bankMapper = mySqlSession.getMapper(BankMapper.class);
        System.out.println("bankMapper.getClass() = " + bankMapper.getClass());
        Bank bank = bankMapper.selectBankById(1);
        System.out.println("bank = " + bank);
    }


}